var userId;
var userName;
var onlineUserMap = new Map();
var onlineUL;
var lotteryMap = new Map();
var lotteryUL;
var socket;

var progress;


function onHtmlLoad() {
    var userCookie = getUserCookie('userName');
    if (userCookie === '') {
        userCookie = getUserName();
    }
    document.getElementById('fieldName').value = userCookie;
    onlineUL = document.getElementById('all_user');
    lotteryUL = document.getElementById('lottery_user');
}

function changeUsername() {
    document.getElementById('fieldName').value = getUserName();
}

//连接
function connServerClick() {

    let value = document.getElementById('fieldName').value;
    if (value === '') {
        alert("名字呢?");
        return;
    }

    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }

    if (window.WebSocket) {
        const ip = "ws://" + document.getElementById('fieldURL').value;
        socket = new WebSocket(ip); //建立连接,并发送信息
        socket.onopen = channelOpen;
        socket.onmessage = channelRead;
        socket.onclose = channelClose;
        socket.onerror = exceptionCatch;

    } else {
        alert("浏览器不支持WebSocket!");
    }
}

function channelOpen(event) {
    console.log("channelOpen .send login cmd");
    loginCmd();
}

function channelRead(event) {
    let resp = JSON.parse(event.data);
    console.log(resp);
    switch (resp.actionId) {
        case 20002:  //通知所有人,有人上线
            onlineUserMap.set(resp.userId, resp);
            freshOnlineUserList();
            break;
        case 20003: //获取所有在线列表
                    // onlineUserList(resp.itemList);
            break;
        case 20004: //登录成功
            userId = resp.userId;
            userName = resp.userName;
            break;
        case 43000:     //获取抽奖列表
            console.log('获取抽奖列表');
            console.log(resp);
            // lottoryUserList(resp.itemList);
            break;

    }

}

function channelClose(event) {

}

function exceptionCatch(event) {

}

//发送登录指令
function loginCmd() {
    let name = document.getElementById('fieldName').value;
    var time = Date.parse(new Date());
    var login = JSON.stringify({
        "userName": name,
        "actionId": 20001,
        "time": time
    });
    sendMessage(login);
}

function freshOnlineUserList() {
    /**
     <li class="fn-clear" data-id="1"
     title="你是猪吗?">
     <span><img src="images/hetu.jpg" width="30" height="30" alt=""/></span>
     <small class="online" title="在线"></small>
     <em>河图</em>
     </li>
     */

    var index = 0;
    for (var [key, item] of onlineUserMap.entries()) {
        //language=HTML
        var li = '<li class="fn-clear" data-id="1"' +
            ' title="你是猪吗?">\n' +
            '    <span><img src="images/hetu.jpg" width="30" height="30" alt=""/></span>\n' +
            '    <small class="online" title="在线"></small>\n' +
            '    <em>河图</em>\n' +
            '</li>';
        onlineUL.appendChild(li);
    }


}

function sendMessage(message) {
    if (!window.WebSocket) {
        return;
    }
    if (socket.readyState === WebSocket.OPEN) {
        socket.send(message);
    } else {
        console.log("WebSocket连接没有建立成功!");
        alert("您还未连接上服务器，请刷新页面重试");
    }
}